'use strict';
/**
 * Created by FreezeSoul on 2016/1/20.
 */
define([
    "app/core/base",
    "app/core/util",
    "../commonMeta",
    "jquery",
    "underscore",
    'loglevel'
], function (Chart, Util, Meta, $, _, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '基础',
                id: 'IframeUrl',
                name: '页面地址',
                enable: false,
                type: 'text',
                value: '',
                default: '',
                link: 'Url',
                tooltip: '页面地址，请输入完整路径，带完整HTTP协议头'
            },
            {
                group: '基础',
                id: 'IframeScroll',
                name: '显示滚动条 ',
                enable: false,
                type: 'checked',
                value: false,
                default: false,
                link: 'Scroll',
                tooltip: '是否显示滚动条'
            },
            {
                group: '基础',
                id: 'IframeBindUrl',
                name: '绑定地址',
                enable: false,
                type: 'textBinding',
                dataset: '',
                ctype: 'dimension',
                column: {},
                tooltip: '页面地址绑定'
            }
        ].concat($.extend(true, [], Meta.commonProperties)),
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var iframe = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._option = {
                Series: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    uuid: '',
                    class: '',
                    display: true,
                    Url: "",
                    Scroll: false,
                    initScript: '',
                    disposeScript: ''
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _render: function (option) {
            $(this._container).empty();
            var $iframe = $('<iframe src="" width="100%" height="100%" frameborder="0"></iframe>');
            $iframe.attr("src", option.Url + "?v=" + window.version).attr("scrolling", option.Scroll ? "" : "no").appendTo(this._container);
            var timeOutId = setTimeout(function () {
                $iframe.css("opacity", "1");
            }, 5000);
            $iframe.css("opacity", "0").load(function () {
                $(this).css("opacity", "1");
                clearTimeout(timeOutId);
            });

            //Util.setCommonMeta(this, this._container, this._option.Option);
            //Util.initChartLifeTime(this, this._container, this._option.Option);

            return $iframe;
        },
        example: function () {
            try {
                var exampleOption = {
                    Url: "http://www.baidu.com",
                    Scroll: true
                };
                this._element = this._render(exampleOption);
            } catch (e) {
                log.error(e);
            }
        },
        _bindData: function (data) {
            var bindProperty = _.findWhere(this._option.Extend.option, {type: 'textBinding'});
            if (!bindProperty || !bindProperty.dataset) return;
            var dataSetCode = bindProperty.dataset;
            var table = data[dataSetCode];
            if (table && table.length > 0 && bindProperty.column) {
                var row = table[0];
                var columnName = bindProperty.column.ColumnName;
                if (columnName) {
                    this._option.Option.Url = row[columnName];
                }
            }
        },
        setData: function (data) {
            try {
                this._readProperty();
                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                ) {
                    this._bindData(data);
                }
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                $(this._element).off().remove();
                //Util.disposeChartLifeTime(this, this._container, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return {
        Chart: iframe,
        Property: property
    };
});